<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {
	//系统首页
    public function index(){
        $uid = I('get.uid',1);
        $class_id = I('get.class_id', 0);
        if($this->check_user($uid, $class_id)){
            $this->allGoods();
        }else{
            $this->error('无效操作！', 'javascript:history.go(-1);');
        }
    }

    public function sindex(){
        $gc = M('goods_class');
        $goods = M('goods');
        $where['pid'] = 0;
        $list = $gc->field('id,pid,name')->where($where)->order('id asc')->select();
        $temp_list = array();
        $str = '';
        foreach($list as $key => &$v){
            $v['son'] =  $goods->field('id,name,picture,price,shop_price,zhekou,unit_id')->where("is_recommend = 1 and class_id = {$v['id']}")->order('utime desc')->select();
            unset($temp_list);
        }
        $this->assign('list', $list);
        $this->assign('index', 'INDEX');
        $this->display();
    }

    public function allGoods(){
        $priceSort = I('get.priceSort', '');
        $saleSort = I('get.saleSort', '');
        $newSort = I('get.newSort', '');
        $zkSort = I('get.zkSort', '');
        $keyword = I('get.keyword', '');
        $class_parent_id = I('get.parent_id');
        $order = '';
        $type = 0;
        if($priceSort != ''){
            $order = 'price desc';
            $type = 1;
        }
        if($saleSort != ''){
            $order = 'sale_count desc';
            $type = 2;
        }
        if($newSort != ''){
            $order = 'ctime desc';
            $type = 3;
        }
        if($zkSort != ''){
            $order = 'zhekou desc';
            $type =4;
        }
        $where = "is_sj = 1";
        if($keyword != ''){
            $where .= " and `name` like '%$keyword%'";
        }

        $class_id = $_SESSION['class_id'];

        if( !empty($class_parent_id) ){
            if($class_parent_id > 0){
                $gc = M('goods_class');
                $list_class = $gc->field(true)->where(array('id' => $class_parent_id))->find();
                $where .= " and class_id = {$list_class['id']}";
            }
            $tid = I('get.tid');
            if( !empty($tid) ){
//            $where = array('is_sj' => 1, 'class_id' => $tid);
                $where .= " and class_id = $tid";
            }
        }else{
            if( !empty($class_id) ){
                $where .= " and class_id = $class_id";
            }
        }


        $page = I('get.page', 0);
        $list =  M('goods')->field('id,name,picture,price,shop_price,zhekou,unit_id')->where($where)->order($order)->limit($page, 10)->select();
        //当商品数量只有一个时，自动进入到商品详细页.
        if( count($list) == 1 ){
            $this->redirect( 'GoodsList/goods_detail', array('id' => $list[0]['id']));
            exit;
        }
        $data = M('goods_class')->field('name')->where(array('id' => $tid))->find();
        $data['uid'] = $_SESSION['userid'];
        $data['shopid'] = $_SESSION['shopid'];
        if($page <= 0){
            $this->assign('list', $list);
            $this->assign('data', $data);
            $this->assign('type', $type);
            $this->assign('index', 'INDEX');
            $this->display('allGoods');
        }else{
            foreach($list as $k => &$v){
                $v['pictureurl'] = get_cover($v['picture'], 'path');
                $v['account_zhekou'] = count_zhekou($v['zhekou']);
				$v['url'] = U('GoodsList/goods_detail', array('id' => $v['id']));
            }
            $this->ajaxReturn(array('status' => true, 'list' => $list));
        }

    }

    public function check_user($user_id, $class_id){
        if($user_id > 0){
            $_SESSION['shopid'] = 10990;//点粉圈微商城ID
            $_SESSION['userid'] = $user_id;
            if($class_id > 0)   $_SESSION['class_id'] = $class_id;
            $clients = M('clients','caitou_','DB_CONFIG1');
            $user_count = $clients->where(array('uid' => $user_id))->count();
            if($user_count <= 0){
                $addData = array(
                    'uid' => $user_id
                );
                if($clients->add($addData)){
					$_SESSION['wechat_id'] = $user_id;
                }else{
                    return false;
                }
            }else{
                $_SESSION['wechat_id'] = $user_id;
            }
            return true;
        }else{
            return false;
        }
    }
}